package pl.edu.agh.io.fx3.filesystem;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.log4j.Logger;

import pl.edu.agh.io.fx3.exceptions.ConfigFileException;
import pl.edu.agh.io.fx3.filesystem.disk.DiskFileSystem;
import pl.edu.agh.io.fx3.saver.dao.FileUtilsDAO;
import pl.edu.agh.io.fx3.treecreator.disk.DiskTreeCreator;


/**
 * File System Factory for returning specific implementation of File System.
 * It can be extended in future by adding other method which returns class
 * implementing data storing in a different way.
 * 
 * @author Jaroslaw Janik
 */
public class FileSystemFactory {
	public static Logger LOGGER;
	
	
	public FileSystemFactory() {
		LOGGER = Logger.getLogger(FileSystemFactory.class);
		LOGGER.debug("FileSystemFactory created.");
	}
	
	/**
	 * Returns new created implementation of File System which store data on
	 * Disk. That new object can be configured by adding path to configuration file.
	 * 
	 * @return New instance of File System.
	 * @throws FileNotFoundException
	 */
	public DiskFileSystem getDiskFileSystem() throws FileNotFoundException, ConfigFileException {
		File file = new File(FileSystemFactory.class.getResource("/config.properties").getFile());

		LOGGER.debug("File " + file + " loaded as configuration file.");
		
		if (!file.exists() || !file.isFile()) {
			LOGGER.error("File " + file + " not found. Throwing FileNotFoundException.");
			throw new FileNotFoundException();
		}
		
		try {
			FileUtilsDAO dao = new FileUtilsDAO();
			LOGGER.debug("New instance of FileUtilsDAO created.");
			
			DiskTreeCreator diskTreeCreator = DiskTreeCreator.getTreeCreator(file, dao);
			LOGGER.debug("New instance of DiskTreeCreator created.");
			
			LOGGER.debug("Creating new DiskFileSystem instance.");
			return new DiskFileSystem(file, dao, diskTreeCreator);
		} catch (ConfigFileException e) {
			LOGGER.error(e);
			throw e;
		}
	}
}
